/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import l1j.server.server.model.L1Character;
import l1j.server.server.model.skill.TargetStatus;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_RangeSkill extends ServerBasePacket {

	private static final String S_RANGE_SKILL = "[S] S_RangeSkill";
	private static AtomicInteger _sequentialNumber = new AtomicInteger(9000000);

	private byte[] _byte = null;

	public static final int TYPE_NODIR = 0;

	public static final int TYPE_DIR = 8;

	private static int calcheading(final int myx, final int myy, final int tx,
			final int ty) {
		int newheading = 0;
		if (tx > myx && ty > myy) {
			newheading = 3;
		}
		if (tx < myx && ty < myy) {
			newheading = 7;
		}
		if (tx > myx && ty == myy) {
			newheading = 2;
		}
		if (tx < myx && ty == myy) {
			newheading = 6;
		}
		if (tx == myx && ty < myy) {
			newheading = 0;
		}
		if (tx == myx && ty > myy) {
			newheading = 4;
		}
		if (tx < myx && ty > myy) {
			newheading = 5;
		}
		if (tx > myx && ty < myy) {
			newheading = 1;
		}
		return newheading;
	}

	public S_RangeSkill(final L1Character cha, 
//			final L1Character[] target,
			final ArrayList<TargetStatus> targetList,
			final int spellgfx, final int actionId, final int type) {
		this.buildPacket(cha, targetList, spellgfx, actionId, type);
	}

	private void buildPacket(final L1Character cha, 
//			final L1Character[] target,
			final ArrayList<TargetStatus> targetList,
			final int spellgfx, final int actionId, final int type) {
		this.writeC(S_OPCODE_RANGESKILLS);
		this.writeC(actionId);
		this.writeD(cha.getId());
		this.writeH(cha.getX());
		this.writeH(cha.getY());
		if (type == TYPE_NODIR) {
			this.writeC(cha.getHeading());
		} else if (type == TYPE_DIR) {
			final int newHeading = calcheading(cha.getX(), cha.getY(),
//					target[0].getX(), 
					targetList.get(0).getTarget().getX(),
//					target[0].getY())
					targetList.get(0).getTarget().getY())
					;
			cha.setHeading(newHeading);
			this.writeC(cha.getHeading());
		}
		this.writeD(_sequentialNumber.incrementAndGet()); // 番号がダブらないように送る。
		this.writeH(spellgfx);
		this.writeC(type); // 0:範囲 6:遠距離 8:範囲&遠距離
		this.writeH(0);
		this.writeH(targetList.size());
//		this.writeH(target.length);
//		for (final L1Character element : target) {
//			this.writeD(element.getId());
//			this.writeC(0x20); // 0:ダメージモーションあり 0以外:なし
//		}
		for (final TargetStatus target : targetList) {
			//System.out.println("TG: "+target.getTarget().getName() + "/" + target.isCalc());
			this.writeD(target.getTarget().getId());
			if (target.isCalc()) {
				this.writeH(0x20);
			} else {
				this.writeH(0x00); // 0x00:無傷害 大於0傷害質
			}
		}
	}
	@Override
	public byte[] getContent() {
		if (this._byte == null) {
			this._byte = this._bao.toByteArray();
		}
		return this._byte;
	}

	@Override
	public String getType() {
		return S_RANGE_SKILL;
	}

}